package dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import formBean.TransitionDayForm;

import bean.Fund;
import bean.FundPriceHistory;
import bean.UserBean;

public class FundPriceHistoryDAO extends DAO<UserBean> {

	public FundPriceHistoryDAO(SessionFactory factory) {
		super(factory);
	}
	
	public ArrayList<String> transitionDayPrice(TransitionDayForm form, List<Fund> funds) {
		ArrayList<String> errors = new ArrayList<String>();
		Transaction tx = null;
		Session session = null;
		try {
			session = this.getSession();
			tx = session.beginTransaction();			

			for (Fund f: funds) {
				FundPriceHistory h = new FundPriceHistory();
				h.setFund(f);
				h.setPrice(form.getPrice(f.getId()));
				h.setPriceDate(form.getDate());			
				session.save(h);
			}
			
			tx.commit();
		} catch (Exception e) {
			if (tx != null)	tx.rollback();
			e.printStackTrace();
			errors.add(e.getMessage());
			return errors;
		} finally {			
			if(session != null)	session.close();
		}
		return errors;
	}

	public List<FundPriceHistory> getFundPriceHistory(Fund fund){
		Session session = null;
		try {
			session = this.getSession();
			Query qr = session.createQuery(" from FundPriceHistory where fundId = '" +fund.getId()+"' ");
			return qr.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			if(session != null)	session.close();
		}
		return null;		
	}

//	public FundPriceHistory getLatestFundPrice(Fund fund)
//	{
//		List<FundPriceHistory> lstFundPriceHistory = getFundPriceHistory(fund);
//
//		Collections.sort(lstFundPriceHistory, new Comparator<FundPriceHistory>() {
//			public int compare(FundPriceHistory m1, FundPriceHistory m2) {
//				return m1.getPriceDate().compareTo(m2.getPriceDate());
//			}
//		});
//
//		return lstFundPriceHistory.get(0);
//	}
	public Date getLatestDate() {
		Session session = null;
		try {
			session = this.getSession();
			Query qr = session.createQuery("select history.priceDate from FundPriceHistory history where history.priceDate is not null");
			List<Date> list = qr.list();
			if (list == null || list.size() == 0) return null;
			Collections.sort(list);
			session.close();
			return list.get(list.size() - 1);
		} catch (HibernateException e) {
			e.printStackTrace();
		} 
		return null;
	}
	
	public boolean create(FundPriceHistory h) {
		Transaction tx = null;
		Session session = null;
		try {				
			session = this.getSession();
			tx = session.beginTransaction();
			session.save(h);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
			return false;
		} finally {
			if(session != null)	session.close();
		}
		
		return true;
	}
}